function directUdls(){
	$.ajax({
		url :interfaceMobileUrl+"/getAddress.json?userid="+userid+"&key="+userkey,
		jsonp: "callback",
	    dataType: "jsonp",
	    success:function(data){
	    	var addresses = eval('(' + data + ')');
			addresses = addresses.list[0]["com.opac.readerHome.domain.Address"];
			$("ul.xuanzedizhi").html("");
			for(var i=0;i<addresses.length;i++){
				var address = addresses[i].province+addresses[i].city+addresses[i].district+addresses[i].detail;
				var li="<li addressId='"+addresses[i].id+"'><a href='#'><p>"+address+"</p></a></li>";
				if(addresses[i].defaultAddress){
					$("#defaultAddress").html(address);
					li = "<li addressId='"+addresses[i].id+"' class='cur'><a href='#'><p>"+address+"</p></a></li>";
				}
				$("ul.xuanzedizhi").append(li);
			}
		}
	});
}
function excuteUdls(){
//	console.error(currentQrCode);
	$.ajax({
		url :interfaceMobileUrl+"/udls/execute.json?userid="+userid+"&key="+userkey+"&uuid="+currentQrCode,
		jsonp: "callback",
	    dataType: "jsonp",
	    beforeSend:function(){
	    	 $.ui.showMask("正在努力执行转借...");
	    },
	    complete:function(){
	    	$.ui.hideMask(); 
	    },
	    success:function(data){
	    	console.error(data);
	    	if(data == "1"){
	    		xfAlert("转入成功");
//	    		$("#udlsBookDetail .headBt").attr("href","#me_shu");
	    		toMyLoan(userid);
//	    		$.ui.loadContent("#me_shu",false,false,"up");
	    		$.ui.loadContent("#directUdlsResult",false,false,"up");
	    		$("#directUdlsResult").attr("udlsType","in");
	    	}else{
	    		alert(data);
	    	}
		}
	});
}
function qrscan(){
	
	cordova.exec(function(qrCode){
		if (qrCode.length == 36)
                {
					$.ajax({
						url :interfaceMobileUrl+"/udls/getInfo.json?userid="+userid+"&key="+userkey+"&uuid="+qrCode,
						jsonp: "callback",
					    dataType: "jsonp",
					    async : false,
					    success:function(data){
					    	if(data.length<20){
					    		xfAlert(data);
					    		return;
					    	}
					    	console.error(data);
					    	data = eval('(' + data + ')');
					    	$("#udlsBookInfo").html("");
					    	data =  data.list[0]["com.opac.readerHome.domain.UdlsHistory"];
					    	if(data.length == undefined){
					    		var book = data;
					    		$("#udlsBookInfo").append("<div class='xq_tup'>"+
				    					"<img isbn='"+book.isbn+"' src='images/default1.jpg'>"+
				    					"</div>"+
				    					"<ul>"+
				    					"<li>"+
				    					"<p>"+book.title+"</p> <span>"+book.author+"</span>"+
				    					"</li>"+
				    			"</ul>");
					    	}else{
					    		for(var i=0;i<data.length;i++){
					    			var book = data[i];
					    			$("#udlsBookInfo").append("<div class='xq_tup'>"+
					    					"<img isbn='"+book.isbn+"' src='images/default1.jpg'>"+
					    					"</div>"+
					    					"<ul>"+
					    					"<li>"+
					    					"<p>"+book.title+"</p> <span>"+book.author+"</span>"+
					    					"</li>"+
					    			"</ul>");
					    		}
					    	}
				    		coverCover();
				    		currentQrCode = qrCode;
				    		$.ui.loadContent("#udlsBookDetail",false,false,"up");
						}
					});
                }else if(qrCode!="canceled"){
//                	$.ui.loadContent("#index",false,false,"up");
                	xfAlert("不是有效的二维码");
//                	af.ui.toggleSideMenu();
				}
				
			}, function(w){
				xfAlert("扫描失败");
//				navigator.notification.alert("fail"+w);
			}, "qrPlugin", "scan", []);
}
$(function(){
	$("#qrScan").click(function(){
		if(userid ==null){
			xfAlert("请先登录");
			$.ui.loadContent("#denglu",false,false,"up");
			return false;
		}
		if(!libtied ){
			xfAlert("请先绑定读者证");
			$.ui.loadContent("#bdduzz",false,false,"up");
			return false;
		}
		qrscan();
	});
	$("#udlsBookDetail .headBt").click(function(){
		excuteUdls();
	});
	$("#toUdlsStep2").click(function(){
		var barcodes = "";
		var gLibcode = "";
		$("#batchUdlsLoanUl li.cur").each(function(){
			var barcode = $(this).attr("barcode");
			barcodes = barcodes+barcode+",";
			gLibcode = $(this).attr("gLibcode");
		});
		if(barcodes == ""){
			$("#toUdlsStep2").attr("href","#");
			xfAlert("请选择您要发布的书籍");
		}else{
			barcodes = barcodes.substring(0,barcodes.length-1);
			generateQrcode(gLibcode,barcodes);
			$("#toReleaseStep2").attr("href","#lijizj_2");
		}
		
	});
	$("#udlsResultUl li img").click(function(){
		$.ui.loadContent("#me_shu",false,false,"up");
	});
	
});
function generateQrcode(gLibcode,barcode){
	$.ajax({
		url :interfaceMobileUrl+"/udls/getQrcode.json?userid="+userid+"&key="+userkey+"&gLibcode="+gLibcode+"&barcodes="+barcode,
		jsonp: "callback",
	    dataType: "jsonp",
	    success:function(data){
			$(".zjbuz2 #udlsQrcode").html("");
			$(".zjbuz2 #udlsQrcode").qrcode(data);
			var int = window.setInterval(function(){
    			$.ajax({
    				url :interfaceMobileUrl+"/udls/isExecuteDone.json?userid="+userid+"&key="+userkey+"&uuid="+data,
					jsonp: "callback",
				    dataType: "jsonp",
				    success:function(data){
				    	if(data == "1"){
				    		window.clearInterval(int);
				    		xfAlert("转出成功");
//				    		$.ui.loadContent("#me_shu",false,false,"up");
				    		toMyLoan(userid);
				    		$.ui.loadContent("#directUdlsResult",false,false,"up");
				    		$("#directUdlsResult").attr("udlsType","out");
				    		return;
				    	}
				    	if(data == "-1"){
				    		window.clearInterval(int);
				    		var href = window.location.href;
				    		if(href.indexOf("#lijizj_2")>0){
				    			toMyLoan(userid);
					    		xfAlert("二维码已经过期");
					    		$.ui.loadContent("#me_shu",false,false,"up");
				    		}
				    		return;
				    	}
				    }
    			});
    		},3000);
			setTimeout(function(){
				window.clearInterval(int);
				if(href.indexOf("#lijizj_2")>0){
		    		xfAlert("二维码已经过期");
		    		$.ui.loadContent("#me_shu",false,false,"up");
	    		}
			},10*60*1000);
		}
	});
}
function directUdlsResult_load(){
	$("#directUdlsResult #udlsResultUl b").html("4");
	var timeLeft = 3;
	var resultInterval=window.setInterval(function(){
		if(timeLeft>0){
			$("#directUdlsResult #udlsResultUl b").html(timeLeft);
			timeLeft--;
		}else{
			var type = $("#directUdlsResult").attr("udlsType");
//			alert(type);
			if(type=="in"){
				$.ui.loadContent("#jieyuels",false,false,"up");
				inHistory();
			}else if(type == "out"){
				$.ui.loadContent("#zjiels",false,false,"up");
				outHistory();
			}
			window.clearInterval(resultInterval);
		}
	},1000);
}